<?php
/**
 * Created by PhpStorm.
 * User: krystal
 * Date: 2019/4/1
 * Time: 16:45
 */

namespace app\common\model;


use think\Cache;

/**
 * 用户习题进度模型
 * Class ZhunleSectionExerciseProgress
 * @package app\common\model
 */
class ZhunleSectionExerciseProgress extends Base
{
    const USER_EXERCISE_PROGRESS = ':user:exercise:progress:';
    const CACHE_TIME = 86400 * 7;//用户章节习题进度缓存
    protected $table = 'wechat_backup_07_18_14_43_00.zhunle_exercise_progress';

    /**
     * 获取用户习题完成状态
     * @param array $map
     * @return array|bool|mixed
     */
    public function getUserExerciseProgress($map = [])
    {
        $csid = $map['csid'];
        $uid = $map['uid'];

        $data = false;

        if ($this->cache_on >= 1)
        {
            $data = Cache::get($uid . self::USER_EXERCISE_PROGRESS . $csid);
        }
        if ($data === false)
        {
            try
            {
                $where = ['csid' => $csid, 'uid' => $uid];

                $data = $this->where($where)->column('exercise_status');

                if ($this->cache_on >= 1)
                {
                    Cache::set($uid . self::USER_EXERCISE_PROGRESS . $csid, $data, self::CACHE_TIME);
                }
            }
            catch (\Exception $e)
            {
                $this->error = '获取习题进度失败';
                return false;
            }
        }
        return $data;
    }

    /**
     * 新增用户习题完成状态
     * @param array $map
     * @return bool
     */
    public function addExerciseProgress($map = [])
    {
        $uid = $map['uid'];
        $csid = $map['csid'];
        $exercise_status = $map['status'];

        $arr = [
            'uid' => $uid,
            'csid' => $csid,
            'exercise_status' => $exercise_status
        ];

        try
        {
            $this->save($arr);

            if ($this->cache_on >= 1)
            {
                $status = [
                    '0' => $exercise_status
                ];

                Cache::set($uid . self::USER_EXERCISE_PROGRESS . $csid, $status, self::CACHE_TIME);
            }
        }
        catch (\Exception $e)
        {
            $this->error = '保存习题进度失败';

            return false;
        }

        return true;
    }


    /**
     * 修改用户习题完成状态
     * @param array $map
     * @return bool
     */
    public function editExerciseProgress($map = [])
    {
        $uid = $map['uid'];
        $csid = $map['csid'];
        $exercise_status = $map['status'];

        try
        {
            $this->save(['exercise_status' => $exercise_status], ['uid' => $uid, 'csid' => $csid]);

            if ($this->cache_on >= 1)
            {
                $status = [
                    '0' => $exercise_status
                ];

                Cache::set($uid . self::USER_EXERCISE_PROGRESS . $csid, $status, self::CACHE_TIME);
            }

        }
        catch (\Exception $e)
        {
            $this->error = '更新习题进度失败';

            return false;
        }

        return true;
    }

}
